package com.vgemv.jsutilitysdk.Application;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.HashMap;
import java.util.Map;

public  abstract  class JsBaseApplication extends Application {
    private static JsBaseApplication _sharedInstance;
    public String mString_appName;


    private HashMap<String,String> mHashMap_deviceInfo;
    private static final String VERSION_NAME = "VersionName";
    private static final String VERSION_CODE = "VersionCode";
    private static final String APP_NAME = "AppName";
    public JsBaseApplicationCallback mCallback;

    /**
     * 系统会创建一个Application对象,这里可以认为是程序最初的起点.
     * 在这里可以初始化相关的类.
     */


    @Override
    public void onCreate() {
        super.onCreate();
        _sharedInstance = this;
        initComponents();
    }

    public static JsBaseApplication sharedInstance() {
        return _sharedInstance;
    }

    //让子类初始化各种数据.
    public abstract void initComponents();



    // 2 Inner
    /**
     * 获取App的相关信息,存放到Map中.
     * @return
     */
    public Map<String, String> deviceInfo() {
        if(mHashMap_deviceInfo == null) {
            mHashMap_deviceInfo = new HashMap<String, String>();
            //对应manifest节点.
            PackageManager pm = JsBaseApplication.sharedInstance().getPackageManager();
            //返回App的包名,对应manifest节点的package属性.
            String packageName = sharedInstance().getPackageName();

            //对应指定manifest的application节点
            ApplicationInfo applicationInfo = null;
            PackageInfo packageInfo = null;
            String appName = null;
            //build号
            int versionCode = 0;
            //版本号
            String versionName = "";
            try {
                packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
                applicationInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
                int appNameResId = applicationInfo.labelRes;
                if (appNameResId != 0) {
                    appName = getString(appNameResId);
                }
                versionCode = packageInfo.versionCode;
                versionName = packageInfo.versionName;

            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            mHashMap_deviceInfo.put(APP_NAME, appName);
            mHashMap_deviceInfo.put(VERSION_CODE, String.format("%d", versionCode));
            mHashMap_deviceInfo.put(VERSION_NAME, versionName);
            mHashMap_deviceInfo.put("设备型号",Build.MODEL);
            mHashMap_deviceInfo.put("API等级", String.valueOf(Build.VERSION.SDK_INT));
            mHashMap_deviceInfo.put("安卓版本", String.valueOf(Build.VERSION.RELEASE));
            mHashMap_deviceInfo.put("品牌",Build.BRAND);
            mHashMap_deviceInfo.put("主板",Build.BOARD);
            mHashMap_deviceInfo.put("CPU类型",Build.CPU_ABI);
            mHashMap_deviceInfo.put("设备参数",Build.DEVICE);
            mHashMap_deviceInfo.put("DISPLAY",Build.DISPLAY);
            mHashMap_deviceInfo.put("硬件设备",Build.FINGERPRINT);
            mHashMap_deviceInfo.put("制造商",Build.MANUFACTURER);
            mHashMap_deviceInfo.put("手机制造商",Build.PRODUCT);
            mHashMap_deviceInfo.put("构造类型",getBuildType());
        }
        return mHashMap_deviceInfo;

    }

    //是否为debug模式.
    public boolean isDebug() {
        PackageManager packageManager = getPackageManager();
        String packageName = getPackageName();
        try{
            ApplicationInfo info = packageManager.getApplicationInfo(packageName , 0);
            return (0 != (info.flags & ApplicationInfo.FLAG_DEBUGGABLE));
        }catch(PackageManager.NameNotFoundException e){
            /*debuggable variable will remain false*/
            return false;
        }
    }

    public String getBuildType() {
        if(isDebug()) {
            return "debug";
        }
        else {
            return "release";
        }
    }

    // 3 Interface
    // 0.0.0(0)
    public String getAppVersion() {
        return String.format("%s(%s)",getVersionName(),getVersionCode());
    }

    public  String getAppName() {
        return deviceInfo().get(APP_NAME);

    }
    //x.x.x
    public  String getVersionName() {
        return deviceInfo().get(VERSION_NAME);
    }
    //自增build
    public  String getVersionCode() {
        return deviceInfo().get(VERSION_CODE);
    }


    /**
     * 不要直接使用其他组件.这样增加了耦合性.
     * 通过代理,将事件代理出去,交给使用者去使用这些组件.
     */
    @Override
    public void onTerminate() {
        super.onTerminate();
        if(mCallback!=null){
            mCallback.onAppTerminate();
        }
    }
}
